function showPage(result){
	$("#Tbody").empty();
    var i=1;
    for(i=0;i<result.length-1;i++){
    	var $trTemp = $("<tr></tr>");
    	$trTemp.attr("id","tr"+i);
    	$trTemp.append("<th>"+ result[i].sbj_name +"</th>");
        
        var $th_sbj_Temp = $("<th>" + result[i].sbj_name + "</th>");
        
        var $th_teh_Temp = $("<th>" + result[i].teh_name + "</th>");
        $th_teh_Temp.attr("id","tehId"+i);
        $th_teh_Temp.attr("name",result[i].teh_id);
        $trTemp.append($th_teh_Temp);
        
        $trTemp.append("<th>"+ result[i].sbj_describe +"</th>");
        
        var $btnDownTemp = $("<button>查看</button>");
        $btnDownTemp.attr("class","btn btn-primary btn-lg choose_btn");
        $btnDownTemp.attr("onclick","find('"+ result[i].sbj_id +"','"+ result[i].teh_id +"')");
        var $thDownTemp = $("<th></th>");
        $thDownTemp.append($btnDownTemp)
        $trTemp.append($thDownTemp);
        
        var $btnTemp = $("<button>确认选择</button>");
        $btnTemp.attr("class","btn btn-primary btn-lg choose_btn");
        $btnTemp.attr("type","button");
        $btnTemp.attr("data-toggle","modal");
        $btnTemp.attr("data-target","#myModal");
        $btnTemp.attr("onclick","choose('"+ result[i].sbj_id+"'," +i+")");
        var $thTemp = $("<th></th>");
        $thTemp.append($btnTemp)
        $trTemp.append($thTemp);
        $trTemp.appendTo("#Tbody");
    } 	
}

$(document).ready(function(){
	page = document.getElementById("nowPage").innerText;
	page = parseInt(page);
	
	lastPage = document.getElementById("sumPage").innerText;
	lastPage = parseInt(lastPage);
	
	$.ajax({url:"subject/list",
		data: { page: "1" ,
			sbj_status:"ct03"},
		success:function(result){
			if(result[result.length-1].stu_status=="st01"){
				showPage(result);
			}
			else{
				$.ajax({url:"choosePro",
					success:function(result){
						$("#sumItem").text(1);
						$("#sumPage").text(1);
						
						var $trTemp = $("<tr></tr>");
				    	$trTemp.append("<th>"+ result.sbj_name +"</th>");
				        
				        var $th_sbj_Temp = $("<th>" + result.sbj_id + "</th>");

				        
				        var $th_teh_Temp = $("<th>" + result.teh_name + "</th>");
				        $th_teh_Temp.attr("name",result.teh_id);
				        $trTemp.append($th_teh_Temp);
				        
				        $trTemp.append("<th>"+ result.sbj_describe +"</th>");
				        
				        
				        var $btnDownTemp = $("<button>查看</button>");
				        $btnDownTemp.attr("class","btn btn-primary btn-lg choose_btn");
				        $btnDownTemp.attr("onclick","find('"+ result.sbj_id +"','"+ result.teh_id +"')");
				        var $thDownTemp = $("<th></th>");
				        $thDownTemp.append($btnDownTemp)
				        $trTemp.append($thDownTemp);
				        
				        var $btnTemp = $("<button>已选择</button>");
				        $btnTemp.attr("class","btn btn-primary btn-lg choose_btn");
				        $btnTemp.attr("type","button");
				        var $thTemp = $("<th></th>");
				        $thTemp.append($btnTemp)
				        $trTemp.append($thTemp);
				        $trTemp.appendTo("#Tbody");
					}
				});
 			}
		}
	});
})


function getInfoByPage(){
	var page = $("#jumpNumTxt").val(); 
	$("#nowPage").text(page);
	$.ajax({url:"subject/list",
		data: { page: page ,
			sbj_status:"ct03"},
		success:function(result){
			showPage(result);
		}
	});
}

//首页
function getfirstPage(){
	page = 1;
	$("#nowPage").text(page);
	$.ajax({url:"subject/list",
		data: { page: page ,
			sbj_status:"ct03"},
		success:function(result){
			showPage(result);
		}
	});
}

//下一页
function getnextPage(){
	page = page + 1;
	$("#nowPage").text(page);
	$.ajax({url:"subject/list",
		data: { page: page ,
			sbj_status:"ct03"},
		success:function(result){
			showPage(result);
		}
	});
}

//上一页
function getlastPage(){
	page = page - 1;
	$("#nowPage").text(page);
	$.ajax({url:"subject/list",
		data: { page: page ,
			sbj_status:"ct03"},
		success:function(result){
			showPage(result);
		}
	});
}

//尾页
function getendPage(){
	page = lastPage;
	$("#nowPage").text(page);
	$.ajax({url:"subject/list",
		data: { page: page ,
			sbj_status:"ct03"},
		success:function(result){
			showPage(result);
		}
	});
}

function choose(sbj_id,i){
	temp_teh_Id = "#tehId"+i;
	this.sbj_id= sbj_id
	teh_id=$(temp_teh_Id).attr("name");
}

function check(){
	$.ajax({
		url:"choose",
		data:{
			sbj_id: sbj_id,
			teh_id: teh_id
		},
		success:function(result){
			window.location.reload();
		},
	});
}

function find(sbj_id,teh_id){
	window.location.href = "downloadSbj?sbj_id=" + sbj_id + "&teh_id=" + teh_id;
}